﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

using AutomationAdmin.ViewModels;

namespace AutomationAdmin.Utils
{
    public class DemoHelper
    {
        public static List<AutomationApp> GenAutomationApps()
        {
            List<AutomationApp> apps = new List<AutomationApp>();
            AutomationApp app;

            app = new AutomationApp();
            app.Name = "EIA Natural Gas";
            app.Region = "EMEA";
            app.Sector = "Energy";
            app.Id = 0;
            apps.Add(app);

            app = new AutomationApp();
            app.Name = "EIA Petroleum";
            app.Region = "EMEA";
            app.Sector = "Energy";
            app.Id = 1;
            apps.Add(app);

            app = new AutomationApp();
            app.Name = "EIA API";
            app.Region = "EMEA";
            app.Sector = "Energy";
            app.Id = 2;
            apps.Add(app);

            app = new AutomationApp();
            app.Name = "EIA Oil Refinery";
            app.Region = "EMEA";
            app.Sector = "Energy";
            app.Id = 3;
            apps.Add(app);

            for (int i = 4; i < 10; i++)
            {
                app = new AutomationApp();
                app.Id = i;

                if (i % 2 == 0)
                    app.Sector = "Energy";
                else
                    app.Sector = "Treasury";

                if (i % 3 == 0)
                    app.Region = "EMEA";
                else if (i % 3 == 1)
                    app.Region = "RAM";
                else
                    app.Region = "ASIA";

                app.Name = app.Region +  " Automation App " + i.ToString();

                apps.Add(app);
            }

            return apps;
        }

        public static List<NewAppRequest> GenAutomationReqs()
        {
            List<NewAppRequest> ret = new List<NewAppRequest>();

            int i = 0;
            for(i = 0; i < 5; i++)
            {
                NewAppRequest req = new NewAppRequest();
                req.Date = DateTime.Now - new TimeSpan(20, 0, 0, 0);
                req.ContactName = "user " + i.ToString();
                req.AutomationName = "Automation App " + i.ToString();

                NewAppRequest.Update update;
                update = new NewAppRequest.Update();
                update.Date = req.Date;
                update.Status = req.Status;
                update.UpdatedBy = req.ContactName;
                req.Updates.Add(update);

                update = new NewAppRequest.Update();
                update.Date = req.Date + new TimeSpan(5 - i, 0, 0, 0);
                update.Status = NewAppRequest.ReqStatus.InProgress;
                update.UpdatedBy = "Dev Lead " + i.ToString();
                req.Updates.Add(update);

                if (i == 3)
                {
                    update = new NewAppRequest.Update();
                    update.Date = req.Date + new TimeSpan(5 - i, 0, 0, 0);
                    update.Status = NewAppRequest.ReqStatus.Halted;
                    update.UpdatedBy = "Product Manager " + i.ToString();
                    req.Updates.Add(update);
                }

                if (i % 3 != 0)
                {
                    update = new NewAppRequest.Update();
                    update.Date = req.Date + new TimeSpan(5 - i, 0, 0, 0);
                    update.Status = NewAppRequest.ReqStatus.Completed;
                    update.UpdatedBy = "admin " + i.ToString();
                    req.Updates.Add(update);
                }
                else
                {
                    update = new NewAppRequest.Update();
                    update.Date = req.Date + new TimeSpan(5 - i, 0, 0, 0);
                    update.Status = NewAppRequest.ReqStatus.Cancelled;
                    update.UpdatedBy = "admin " + i.ToString();
                    req.Updates.Add(update);
                }

                ret.Add(req);
            }

            for (; i < 9; i++)
            {
                NewAppRequest req = new NewAppRequest();
                req.Date = DateTime.Now - new TimeSpan(20, 0, 0, 0);
                req.ContactName = "user " + i.ToString();
                req.AutomationName = "Automation App " + i.ToString();

                NewAppRequest.Update update;
                update = new NewAppRequest.Update();
                update.Date = req.Date;
                update.Status = req.Status;
                update.UpdatedBy = req.ContactName;
                req.Updates.Add(update);

                update = new NewAppRequest.Update();
                update.Date = req.Date + new TimeSpan(9 - i, 0, 0, 0);
                update.Status = NewAppRequest.ReqStatus.InProgress;
                update.UpdatedBy = "Dev Lead " + i.ToString();
                req.Updates.Add(update);

                ret.Add(req);
            }

            return ret;
        }

        public static List<SafeUser> GenUsers()
        {
            List<SafeUser> users = new List<SafeUser>();

            for (int i = 0; i < 50; i++)
            {
                SafeUser u = new SafeUser();
                u.Name = "User " + i.ToString();
                u.SafeId = (125886 + i).ToString("0000000");
                users.Add(u);
            }

            return users;
        }

        public static List<UserPermission> GenUserPermissions(
            IList<SafeUser> users, IList<AutomationApp> apps)
        {
            List<UserPermission> permissions = new List<UserPermission>();
            int num = 0;

            for (int i = 0; i < apps.Count; i++)
            {
                int num0 = num;
                for (int j = num; j < users.Count; j++)
                {
                    UserPermission p = new UserPermission();
                    p.User = users[j];
                    p.AutomationApp = apps[i];
                    permissions.Add(p);
                    num++;

                    if (num == users.Count - 1)
                        num = 0;

                    if (j - num0 > 5)
                        break;
                }
            }

            return permissions;
        }

        public static List<AccessAppRequest> GenAccessAppRequests(
            IList<SafeUser> users, IList<AutomationApp> apps)
        {
            List<AccessAppRequest> ret = new List<AccessAppRequest>();
            int num = 0;

            for (int i = apps.Count - 1; i >= 0; i--)
            {
                int num0 = num;
                for (int j = num; j < users.Count; j++)
                {
                    AccessAppRequest r = new AccessAppRequest();
                    r.User = users[j];
                    r.App = apps[i];
                    r.Date = DateTime.Now - new TimeSpan(10, 0, 0, 0);
                    ret.Add(r);
                    num++;

                    if (num == users.Count - 1)
                        num = 0;

                    if (j - num0 > 5)
                        break;
                }
            }

            return ret;
        }
    }
}
